import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import loginCode from '@/views/login/components/login_code'
import { removeToken } from '@/utils/auth'
import Layout from '@/layout'
import store from '@/store'
const routeChange = (to, form, next) => {
  store.commit('login/CHANGE_FLAG', false)
  // removeToken()
  resetRouter()
  // localStorage.clear()
  next()
}
export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
    beforeEnter: routeChange,
    hidden: true
  },
  // {
  //   path: '/home',
  //   name: 'home',
  //   component: home,
  //   hidden: true
  // },
  {
    path: '/testPage',
    name: 'testPage',
    component: () => import('@/views/testPage/index'),
    hidden: true
  },
  {
    path: '/scriptPage',
    name: 'scriptPage',
    component: () => import('@/views/scriptPage/index'),
    hidden: true
  },
  {
    path: '/testAnaly',
    name: 'testAnaly',
    component: () => import('@/views/testAnaly/index'),
    hidden: true
  },
  {
    path: '/cloudPictureView',
    hidden: true,
    component: Layout,
    children: [
      {
        path: "",
        name: "cloudPictureView",
        component: () => import('@/views/chartOverview/cloudPictureView'),
      }
    ],
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/login_code',
    component: loginCode,
    hidden: true
  },
  {
    path: '/',
    redirect: '/chartOverview',
    hidden: true
  },
]


const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
